﻿using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace Axiom.CodeManifold
{
	public class ServiceDefinition
	{
		private string _aggregate = string.Empty;
		private string _comments;
		private Guid _id;
		private List<MethodDefinition> _methods = new List<MethodDefinition>();
		private string _name;

		private List<CodePropertyEx> _repositories = new List<CodePropertyEx>();

		public string Aggregate
		{
			get
			{
				if (string.IsNullOrWhiteSpace(_aggregate))
				{
					Regex regex = new Regex(@"^I(.*)Service$");
					Match m = regex.Match(Name);
					if (m.Success)
					{
						_aggregate = m.Groups[1].ToString();
					}
					else
					{
						throw new TbuNamingConventionException(string.Format("Critical Exception: The Service Name {0} does not conform to the TBU naming convention.", Name));
					}
				}
				return _aggregate;
			}
		}

		public string Comments
		{
			get { return _comments; }
			set { _comments = value; }
		}

		public string ImplementationName
		{
			get
			{
				Regex regex = new Regex(@"^I(.*Service)$", RegexOptions.Compiled);
				if (string.IsNullOrWhiteSpace(Name) || !regex.Match(Name).Success)
				{
					return string.Empty;
				}
				return regex.Match(Name).Groups[1].ToString();
			}
		}

		public List<MethodDefinition> Methods { get { return _methods; } set { _methods = value; } }

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public List<CodePropertyEx> Repositories
		{
			get { return _repositories; }
			set { _repositories = value; }
		}

		public override string ToString()
		{
			return string.Format("The {0} Service: {1}", Aggregate, Name);
		}
	}
}
